package com.kehutong.admin.controler;

import com.kehutong.admin.entity.SystemTag;
import com.kehutong.admin.service.SystemTagService;
import com.kehutong.common.entity.Root;
import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONArray;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.util.Objects;
import org.coraframework.util.UUIDGenerator;

import java.io.IOException;
import java.util.List;

/**
 * @author: liukunlong
 * @date: 2021-07-28 11:52
 */
@MvcAction
@ReqMapping("/admin/sys/tag")
public class SystemTagController {
    static final Logger logger = LoggerFactory.getLogger(SystemTagService.class);


    @Inject
    private JdbcSession jdbcSession;

    @Inject
    private SystemTagService systemTagService;

    /**
     * 根据条件获取标签列表
     *
     * @param jsonObject 查询对象
     * @return 住户汽车列表
     * @throws IOException 操作异常
     */
    @HasPermission("sys:tag:view")
    @ReqMapping("/page")
    public Page<SystemTag> page(JSONObject jsonObject) throws Exception {
        return systemTagService.page(jsonObject);
    }

    @HasPermission("sys:tag:view")
    @ReqMapping("/get")
    public Object get(SystemTag systemTagGroup) {
        return systemTagService.get(systemTagGroup);
    }


    @HasPermission("sys:tag:edit")
    @Bean(newInstance = true, copy = true)
    @ReqMapping("/save")
    public Object save(SystemTag systemTag) {
        return systemTagService.save(systemTag);
    }


    @HasPermission("sys:tag:edit")
    @Bean(copy = true)
    @ReqMapping("/update")
    public Object update(SystemTag systemTag) throws Exception {
        return systemTagService.update(systemTag);
    }

    @HasPermission("sys:tag:edit")
    @ReqMapping("/delete")
    public Object delete(SystemTag systemTag) {
        return systemTagService.delete(systemTag);
    }

    /**
     * 根据条件获取标签组列表
     *
     * @param jsonObject 查询对象
     * @return 住户汽车列表
     * @throws IOException 操作异常
     */
    @HasPermission("sys:tag:view")
    @ReqMapping("/group/page")
    public Page<SystemTag> groupPage(JSONObject jsonObject) throws Exception {
        return systemTagService.groupPage(jsonObject);
    }


    @HasPermission("sys:tag:view")
    @ReqMapping("/group/list")
    public Object groupList(JSONObject jsonObject) throws Exception {
        return systemTagService.groupList(jsonObject);
    }


    @HasPermission("sys:tag:view")
    @ReqMapping("/group/get")
    public Object groupGet(SystemTag systemTagGroup) {
        return systemTagService.groupGet(systemTagGroup);
    }


    @HasPermission("sys:tag:edit")
    @Bean(newInstance = true, copy = true)
    @ReqMapping("/group/save")
    public Object groupSave(JSONObject jsonObject) {
        String name = jsonObject.getString("name");
        JSONArray jsonArray = jsonObject.getJSONArray("childList");
        SystemTag systemTagGroup = Pool.newInstance(SystemTag.class, UUIDGenerator.randomUUID());
        systemTagGroup.setPid(Root.ROOT_ID);
        systemTagGroup.setGroup(true);
        systemTagGroup.setName(name);
        systemTagGroup.setCreateBy(Root.ROOT_ID);
        systemTagGroup.setCreateName("系统");
        systemTagGroup.setSort(1);
        List<SystemTag> childList = JSONObject.parseArray(jsonArray.toJSONString(), SystemTag.class);
        systemTagGroup.setChildList(childList);
        return systemTagService.groupSave(systemTagGroup);
    }


    @HasPermission("sys:tag:edit")
    @Bean(copy = true)
    @ReqMapping("/group/update")
    public Object groupUpdate(JSONObject jsonObject) throws Exception {
        JSONArray jsonArray = jsonObject.getJSONArray("childList");
        if(Objects.nonEmpty(jsonArray)){
            for (Object tagJsonObj : jsonArray) {
                JSONObject tagJson = (JSONObject) tagJsonObj;
                tagJson.remove("type");
            }
        }
        jsonObject.remove("childList");
        jsonObject.remove("type");
        SystemTag systemTagGroup = JSONObject.parseObject(jsonObject.toJSONString(), SystemTag.class);
        List<SystemTag> childList = JSONObject.parseArray(jsonArray.toJSONString(), SystemTag.class);
        systemTagGroup.setChildList(childList);
        return systemTagService.groupUpdate(systemTagGroup);
    }


    @HasPermission("sys:tag:edit")
    @ReqMapping("/group/delete")
    public Object groupDelete(SystemTag systemTagGroup) {
        return systemTagService.groupDelete(systemTagGroup);
    }
}
